package com.sgb.merchant.widget;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.link.entity.merchant.IndustryCategoryEntity;
import com.sgb.link.entity.merchant.IndustryType;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantPopwindowFilterProjectBinding;
import com.sgb.merchant.databinding.MerchantPopwindowFilterTypeBinding;
import com.sgb.merchant.view.adapter.ItemTypeAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;

import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/9/17
 * @Description: 底部行业四级分类弹框
 */
public class TypeFilterPopWindow extends BasePopWindow implements View.OnClickListener {

    MerchantPopwindowFilterTypeBinding mBinding;

    List<IndustryCategoryEntity> mDataList;
    List<IndustryCategoryEntity> mIndustryType,mProjecType,mAllProjectType,mAllThirdType,mThirdType,mAllFourType,mFourType;
    ItemTypeAdapter mFirstAdapter,mSecondAdapter,mThirdAdapter,mFourAdapter;
    boolean isFirstOpen,isProjectOpen,isType3Open,isType4Open;
    IndustryType industryType;

    public TypeFilterPopWindow(Context context) {
        super(context, DIR_DOWN_UP);

    }
    public void setDataList(List<IndustryCategoryEntity> mDataList){
        this.mDataList=mDataList;
        if(mDataList.size()>12) {
            mBinding.industryMore.setVisibility(View.VISIBLE);
            mIndustryType= mDataList.subList(0,12);
        }else{
            mIndustryType=mDataList;
            mBinding.industryMore.setVisibility(View.GONE);
        }
        mFirstAdapter.setDatas(mIndustryType);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_re_cancel) {
            dismiss();
        } else if (id == R.id.tv_ok) {
            if(industryType==null){
                MToast.showToast(mContext,"请选择行业分类！");
                return;
            }
            if (onResultCallBack != null) {
                onResultCallBack.result(industryType);
                dismiss();
            }
        } else if (id == R.id.industry_more) {
            if (isFirstOpen) {
                isFirstOpen = false;
                mBinding.industryMore.setImageResource(R.mipmap.merchant_common_shouqi);
                mFirstAdapter.setDatas(mIndustryType);
            } else {
                isFirstOpen = true;
                mBinding.industryMore.setImageResource(R.mipmap.merchant_common_zhankai);
                mFirstAdapter.setDatas(mDataList);
            }
        } else if (id == R.id.project_more) {
            if (isProjectOpen) {
                isProjectOpen = false;
                mBinding.projectMore.setImageResource(R.mipmap.merchant_common_shouqi);
                mSecondAdapter.setDatas(mProjecType);
            } else {
                isProjectOpen = true;
                mBinding.projectMore.setImageResource(R.mipmap.merchant_common_zhankai);
                mSecondAdapter.setDatas(mAllProjectType);
            }
        } else if (id == R.id.stage_more) {
            if (isType3Open) {
                isType3Open = false;
                mBinding.stageMore.setImageResource(R.mipmap.merchant_common_shouqi);
                mThirdAdapter.setDatas(mThirdType);
            } else {
                isType3Open = true;
                mBinding.stageMore.setImageResource(R.mipmap.merchant_common_zhankai);
                mThirdAdapter.setDatas(mAllThirdType);
            }

        } else if (id == R.id.revenue_more) {
            if (isType4Open) {
                isType4Open = false;
                mBinding.revenueMore.setImageResource(R.mipmap.merchant_common_shouqi);
                mFourAdapter.setDatas(mFourType);
            } else {
                isType4Open = true;
                mBinding.revenueMore.setImageResource(R.mipmap.merchant_common_zhankai);
                mFourAdapter.setDatas(mAllFourType);
            }
        }

    }

    @Override
    protected int popLayout() {
        return R.layout.merchant_popwindow_filter_type;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding= (MerchantPopwindowFilterTypeBinding) holder.getmBinding();
        mBinding.setOnClick(this);
        mBinding.industryRecyclerView.setLayoutManager(new GridLayoutManager(mContext,4));
        mBinding.recyclerViewRevenue.setLayoutManager(new GridLayoutManager(mContext,4));
        mBinding.rvType3List.setLayoutManager(new GridLayoutManager(mContext,4));
        mBinding.projectRecyclerView.setLayoutManager(new GridLayoutManager(mContext,4));
        mFirstAdapter = new ItemTypeAdapter(mContext, null);

        mFirstAdapter.setDefItem(-1);
        mFirstAdapter.setItemSelect(true);
        mBinding.industryRecyclerView.setAdapter(mFirstAdapter);
        mFirstAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                mBinding.llProjectType.setVisibility(View.GONE);
                mBinding.llType3.setVisibility(View.GONE);
                mBinding.llType4.setVisibility(View.GONE);
                mSecondAdapter.setDefItem(-1);
                industryType=new IndustryType();
                if(entity!=null) {
                    industryType.setIndustryCategoryFirstNo(Long.valueOf(entity.getCategoryNo()));
                    industryType.setIndustryCategoryFirstName(entity.getCategoryName());
                }

                List<IndustryCategoryEntity> mChildren= (List<IndustryCategoryEntity>) entity.getSubChildren();
                if(mChildren!=null){
                    if(mChildren.size()>0) {
                        mBinding.llProjectType.setVisibility(View.VISIBLE);
                        mAllProjectType=mChildren;
                        if(mChildren.size()>12){
                            mBinding.projectMore.setVisibility(View.VISIBLE);
                            mProjecType=mChildren.subList(0,12);
                            mSecondAdapter.setDatas(mProjecType);
                        }else{
                            mBinding.projectMore.setVisibility(View.GONE);
                            mSecondAdapter.setDatas(mChildren);
                        }
                    }

                }
            }
        });
        mSecondAdapter=new ItemTypeAdapter(mContext,null);
        mSecondAdapter.setDefItem(-1);
        mSecondAdapter.setItemSelect(true);
        mBinding.projectRecyclerView.setAdapter(mSecondAdapter);
        mSecondAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                mBinding.llType3.setVisibility(View.GONE);
                mBinding.llType4.setVisibility(View.GONE);
                mThirdAdapter.setDefItem(-1);
                if(entity!=null) {
                    industryType.setIndustryCategorySecondNo(Long.valueOf(entity.getCategoryNo()));
                    industryType.setIndustryCategorySecondName(entity.getCategoryName());
                }
                industryType.setIndustryCategoryThreeName("");
                industryType.setIndustryCategoryFourName("");

                List<IndustryCategoryEntity> mChildren= (List<IndustryCategoryEntity>) entity.getSubChildren();
                if(mChildren!=null){
                    if(mChildren.size()>0) {
                        mBinding.llType3.setVisibility(View.VISIBLE);
                        mAllThirdType=mChildren;
                        if(mChildren.size()>12){
                            mBinding.stageMore.setVisibility(View.VISIBLE);
                            mThirdType=mChildren.subList(0,12);
                            mThirdAdapter.setDatas(mThirdType);
                        }else{
                            mBinding.stageMore.setVisibility(View.GONE);
                            mThirdAdapter.setDatas(mAllThirdType);

                        }
                    }

                }
            }
        });

        mThirdAdapter=new ItemTypeAdapter(mContext,mAllThirdType);
        mThirdAdapter.setDefItem(-1);
        mThirdAdapter.setItemSelect(true);
        mBinding.rvType3List.setAdapter(mThirdAdapter);
        mThirdAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                mBinding.llType4.setVisibility(View.GONE);
                mFourAdapter.setDefItem(-1);
                if(entity!=null) {
                    industryType.setIndustryCategoryThreeNo(Long.valueOf(entity.getCategoryNo()));
                    industryType.setIndustryCategoryThreeName(entity.getCategoryName());
                }
                industryType.setIndustryCategoryFourName("");
                
                List<IndustryCategoryEntity> mChildren= (List<IndustryCategoryEntity>) entity.getSubChildren();
                if(mChildren!=null){
                    if(mChildren.size()>0) {
                        mBinding.llType4.setVisibility(View.VISIBLE);
                        mAllFourType=mChildren;
                        if(mChildren.size()>12){
                            mBinding.revenueMore.setVisibility(View.VISIBLE);
                            mFourType=mChildren.subList(0,12);
                            mFourAdapter.setDatas(mFourType);
                        }else{
                            mBinding.revenueMore.setVisibility(View.GONE);
                            mFourAdapter.setDatas(mChildren);
                        }
                    }

                }
            }
        });


        mFourAdapter=new ItemTypeAdapter(mContext,null);
        mFourAdapter.setDefItem(-1);
        mFourAdapter.setItemSelect(true);
        mBinding.recyclerViewRevenue.setAdapter(mFourAdapter);
        mFourAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
            @Override
            public void onItemClick(IndustryCategoryEntity entity, int position) {
                if(entity!=null) {
                    industryType.setIndustryCategoryFourNo(Long.valueOf(entity.getCategoryNo()));
                    industryType.setIndustryCategoryFourName(entity.getCategoryName());
                }
            }
        });
    }

}
